package com.admin.action;

import com.alibaba.fastjson.JSON;
import com.uni.entity.Commodity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * --------------project-------------
 *
 * @Author Unirithe  - Mr.Chao
 * - CSDN:
 * https://blog.csdn.net/Unirithe
 * - GitHub:
 * https://github.com/unirithe
 * @create 2021/12/29 10:05
 * ------------------------------------------
 */
@WebServlet(name = "etReadCommoidty", urlPatterns = "/etReadCommoidty")
public class etReadCommoidty extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        resp.setCharacterEncoding("UTF-8");
        List<Commodity> commodityList = (List<Commodity>) session.getAttribute("commodityList");
        Map<String, Integer> map = new HashMap<>();
        for (Commodity commodity : commodityList) {
            String typeName = commodity.getTypeName();
            map.put(typeName, map.getOrDefault(typeName, 0) + 1);
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }
}
